﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    public class NhapHangDA
    {
        public NhapHangDA()
        {

        }

        public int Insert(NhapHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaHoaDon", entity.MaHoaDon);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaNCC", entity.MaNCC);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("TongTien", entity.TongTien);
                pb.AddParameter("GhiChu", entity.GhiChu);
                return (int)DBFactory.Database.ExecuteNonQuery("NhapHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm thêm 1 thông tin nhập hàng có transaction
        /// </summary>
        /// <returns></returns>
        int InsertTS(NhapHangEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaHoaDon", entity.MaHoaDon);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaNCC", entity.MaNCC);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("TongTien", entity.TongTien);
                pb.AddParameter("GhiChu", entity.GhiChu);
                return (int)DBFactory.Database.ExecuteNonQueryGetID("NhapHang_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm thêm tất cả thông tin của 1 hóa đơn Nhập hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertTS(NhapHangEntity entity)
        {
            try
            {
                DbConnector conn = DBFactory.Database.GetConnector();
                System.Data.Common.DbTransaction tran = DBFactory.Database.CreateTransaction(conn);

                int idnh = this.InsertTS(entity, conn, tran);
                if (idnh == 0)
                    return 0;
                int IDmodel = 0;

                int countModel = entity.ListModel.Count;

                for (int i = 0; i < countModel; i++)
                {
                    //
                    ModelMatHangEntity model = entity.ListModel[i];
                    IDmodel = ModelMatHangDA.InsertTS(model, conn, tran);
                    //
                    if (IDmodel == 0)
                        return 0;
                    ChiTietNhapHangEntity chitiet = new ChiTietNhapHangEntity();
                    chitiet = entity.ListChiTietNhap[i];
                    chitiet.MaHDNH = idnh;
                    chitiet.MaModelMH = IDmodel;
                    if (ChiTietNhapHangDA.Insert(chitiet, conn, tran) == 0)
                        return 0;
                    //
                    int countmathang = model.ListMatHang.Count;

                    for (int j = 0; j < countmathang; j++)
                    {
                        MatHangEntity mathang = model.ListMatHang[j];
                        mathang.MaHoaDonNhap = idnh;
                        mathang.MaModelMH = IDmodel;
                        if (MatHangDA.InsertTS(mathang, conn, tran) == 0)
                            return 0;
                    }
                }
                DBFactory.Database.CommitTransaction(conn, tran);
                return idnh;

            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm cập nhật thô
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(NhapHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("MaNhapHang", entity.MaNhapHang);
                pb.AddParameter("MaHoaDon", entity.MaHoaDon);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaNCC", entity.MaNCC);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("TongTien", entity.TongTien);
                pb.AddParameter("GhiChu", entity.GhiChu);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("NhapHang_UpDate", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_All(bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("NhapHang_FindAll", pb.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        public DataTable Find_ID(int id)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("ID", id);

                dt = DBFactory.Database.FillDataTable("NhapHang_Find_ID", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        DataTable GetInfoNhapHang_ID(int MaNhapHang)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaNhapHang", MaNhapHang);

                dt = DBFactory.Database.FillDataTable("NhapHang_Find_MaNhapHang", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaNhapHang"></param>
        /// <returns></returns>
        public NhapHangEntity GetAllInfo_ID(int MaNhapHang)
        {
            try
            {
                List<NhapHangEntity> nhaphangs = ConvertToList(this.GetInfoNhapHang_ID(MaNhapHang));

                NhapHangEntity nhaphang = nhaphangs[0];

                ChiTietNhapHangDA chitietda = new ChiTietNhapHangDA();

                List<ChiTietNhapHangEntity> chitiets = chitietda.ConvertToList(chitietda.GetInfo_ByMaNhapHang(MaNhapHang));

                List<ModelMatHangEntity> models = new List<ModelMatHangEntity>();

                ModelMatHangDA modelda = new ModelMatHangDA();

                MatHangDA mathangda = new MatHangDA();

                foreach (var chitiet in chitiets)
                {
                    ModelMatHangEntity model = modelda.ConvertToList(modelda.Find_ID(chitiet.MaModelMH, false))[0];
                    models.Add(model);
                }

                foreach (var model in models)
                {
                    List<MatHangEntity> mathangs = new List<MatHangEntity>();
                    mathangs = mathangda.ConvertToList(mathangda.Find_MaModelMH_MaNhapHang(model.MaModelMH, MaNhapHang, false));
                    model.ListMatHang = mathangs;
                }

                nhaphang.ListChiTietNhap = chitiets;

                nhaphang.ListModel = models;

                return nhaphang;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<NhapHangEntity> ConvertToList(DataTable dt)
        {
            List<NhapHangEntity> list = new List<NhapHangEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int ma = (int)dt.Rows[i][0];
                string mahd = dt.Rows[i][1].ToString();
                int manv = (int)dt.Rows[i][2];
                int mancc = (int)dt.Rows[i][3];
                DateTime ngaylap = (DateTime)dt.Rows[i][4];
                decimal tong = (decimal)dt.Rows[i][5];
                string ghichu = dt.Rows[i][6].ToString();

                NhapHangEntity a = new NhapHangEntity(ma, mahd, manv, mancc, ngaylap, tong, ghichu);
                list.Add(a);
            }
            return list;
        }
    }
}
